package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.apiResult.Result;
import com.ruoyi.common.utils.CommonUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.system.bo.ZsyyCreateQrCodeAddBo;
import com.ruoyi.system.bo.ZsyyCreateQrCodeQueryBo;
import com.ruoyi.system.bo.ZsyyCreateQrCodeEditBo;
import com.ruoyi.system.domain.ZsyyCreateQrCode;
import com.ruoyi.system.mapper.ZsyyCreateQrCodeMapper;
import com.ruoyi.system.vo.ZsyyCreateQrCodeVo;
import com.ruoyi.system.service.IZsyyCreateQrCodeService;
import com.ruoyi.system.mapper.ZsyyCreateQrCodeMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 二维码数量Service业务层处理
 *
 * @author wangghua
 * @date 2021-08-09
 */
@Service
public class ZsyyCreateQrCodeServiceImpl extends ServicePlusImpl<ZsyyCreateQrCodeMapper, ZsyyCreateQrCode> implements IZsyyCreateQrCodeService {
	@Autowired
	private ZsyyCreateQrCodeMapper iZsyyCreateQrCodeMapper;
	@Autowired
	private GetCommunityIdImpl getCommunityIdImpl;

	@Override
	public ZsyyCreateQrCodeVo queryById(Long id){
		return getVoById(id, ZsyyCreateQrCodeVo.class);
	}

	@Override
	public TableDataInfo<ZsyyCreateQrCodeVo> queryPageList(ZsyyCreateQrCodeQueryBo bo) {
		PagePlus<ZsyyCreateQrCode, ZsyyCreateQrCodeVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyCreateQrCodeVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<ZsyyCreateQrCodeVo> queryList(ZsyyCreateQrCodeQueryBo bo) {
		return listVo(buildQueryWrapper(bo), ZsyyCreateQrCodeVo.class);
	}

	private LambdaQueryWrapper<ZsyyCreateQrCode> buildQueryWrapper(ZsyyCreateQrCodeQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<ZsyyCreateQrCode> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getCommunityId() != null, ZsyyCreateQrCode::getCommunityId, bo.getCommunityId());
		lqw.eq(bo.getNum() != null, ZsyyCreateQrCode::getNum, bo.getNum());
		lqw.eq(bo.getBuyNum() != null, ZsyyCreateQrCode::getBuyNum, bo.getBuyNum());
		lqw.eq(bo.getDefaultNum() != null, ZsyyCreateQrCode::getDefaultNum, bo.getDefaultNum());
		lqw.eq(bo.getDefaultMoney() != null, ZsyyCreateQrCode::getDefaultMoney, bo.getDefaultMoney());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(ZsyyCreateQrCodeAddBo bo) {
		ZsyyCreateQrCode add = BeanUtil.toBean(bo, ZsyyCreateQrCode.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(ZsyyCreateQrCodeEditBo bo) {
		if (CommonUtil.isEmpty(getCommunityIdImpl.getLoginUser().getCommunityId())){
			bo.setCommunityId(0L);
		}else {
			bo.setCommunityId(getCommunityIdImpl.getLoginUser().getCommunityId());
		}
		Boolean flag = false;
		List<ZsyyCreateQrCode> codeList = iZsyyCreateQrCodeMapper.selectByCommunityId(bo.getCommunityId());
		if(codeList.isEmpty()) {
			//添加信息
			bo.setUpdatetime(new Date());
			bo.setCreatetime(new Date());
			bo.setNum(0L);
			bo.setBuyNum(0L);
			ZsyyCreateQrCode add = BeanUtil.toBean(bo, ZsyyCreateQrCode.class);
			save(add);
			flag = true;
		}else {
			bo.setUpdatetime(new Date());
			bo.setDefaultNum(bo.getDefaultNum());
			bo.setDefaultMoney(bo.getDefaultMoney());
			bo.setId(codeList.get(0).getId());
			ZsyyCreateQrCode update = BeanUtil.toBean(bo, ZsyyCreateQrCode.class);
			updateById(update);
			flag = true;
		}
		return flag;
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(ZsyyCreateQrCode entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
